<!-- 港口/车站-->
<template>
  <basic-container>
    <avue-crud
      :option="option"
      :table-loading="loading"
      :data="data"
      :page.sync="page"
      v-model="form"
      ref="crud"
      @search-change="searchChange"
      @search-reset="searchReset"
      @current-change="currentChange"
      @size-change="sizeChange"
      @refresh-change="refreshChange"
      @on-load="onLoad"
      @row-update="rowUpdate"
      @row-save="rowSave"
      @row-del="rowDel"
      @selection-change="selectionChange"
    >
      <template slot="menuLeft">
        <el-button
          type="danger"
          icon="el-icon-delete"
          size="small"
          @click="batchDelete"
          >删除</el-button
        >
      </template>
    </avue-crud>
  </basic-container>
</template>

<script>
import { getList, update, deleteStation } from "@/api/basedata/station";

export default {
  data() {
    return {
      form: {},
      query: {},
      loading: true,
      page: {
        pageSize: 10,
        currentPage: 1,
        total: 0,
      },
      option: {
        height: "auto",
        calcHeight: 30,
        tip: false,
        selection: true,
        searchShow: false,
        searchMenuSpan: 6,
        border: true,
        labelWidth: "180",
        menuWidth: "250",
        column: [
          {
            label: "港口/车站类型",
            searchLabelWidth: "120",
            prop: "type",
            type: "select",
            props: {
              label: "value",
              value: "key",
            },
            search: true,
            dicUrl: `/api/blade-tms/common/SelectEnum?name=StationType`,
            rules: [
              {
                required: true,
                message: "请选择港口/车站类型",
                trigger: "change",
              },
            ],
          },
          {
            label: "编码",
            prop: "code",
            search: true,
            rules: [
              {
                required: true,
                message: "请输入编码",
                trigger: "blur",
              },
            ],
          },
          {
            label: "描述",
            prop: "describe",
            search: true,
            rules: [
              {
                required: true,
                message: "请输入描述",
                trigger: "blur",
              },
            ],
          },
        ],
      },
      data: [],
      selectionList: [], //选择数据
    };
  },
  computed: {},
  methods: {
    searchReset() {
      this.query = {};
      this.onLoad(this.page);
    },
    searchChange(params, done) {
      this.query = params;
      this.page.currentPage = 1;
      this.onLoad(this.page, params);
      done();
    },
    currentChange(currentPage) {
      this.page.currentPage = currentPage;
    },
    sizeChange(pageSize) {
      this.page.pageSize = pageSize;
    },
    refreshChange() {
      this.onLoad(this.page, this.query);
    },
    onLoad(page, params = {}) {
      let query = Object.assign(params, this.query);
      this.loading = true;
      getList(page.currentPage, page.pageSize, query).then((res) => {
        const data = res.data.data;
        this.page.total = data.total;
        this.data = data.records;
        this.loading = false;
        this.$nextTick(() => {
          this.$refs.crud.$refs.table.doLayout();
        });
      });
    },
    //编辑
    rowUpdate(row, index, done, loading) {
      update(row).then(
        () => {
          this.onLoad(this.page);
          this.$message({
            type: "success",
            message: "操作成功!",
          });
          done();
        },
        (error) => {
          window.console.log(error);
          loading();
        }
      );
    },
    //新增
    rowSave(form, done, loading) {
      loading();
      update(form).then(
        ({ data }) => {
          if (data.code === 200 && data.success) {
            this.onLoad(this.page);
            this.$message({
              type: "success",
              message: "操作成功!",
            });
            done(form);
          }
        },
        (error) => {
          window.console.log(error);
          loading();
        }
      );
    },
    //删除
    rowDel(form) {
      this.delSupercargo([form.id]);
    },
    //批量删除
    batchDelete() {
      if (this.selectionList.length <= 0) {
        this.$message({
          message: "请选择港口/车站",
          type: "warning",
        });
        return false;
      }
      let ids = this.selectionList.map((item) => {
        return item.id;
      });
      this.delSupercargo(ids.join(","));
    },
    delSupercargo(ids) {
      this.$confirm("是否确定删除所选港口/车站?", "提示", {
        confirmButtonText: "确定",
        cancelButtonText: "取消",
        type: "warning",
      })
        .then(() => {
          deleteStation(ids).then(
            () => {
              this.onLoad(this.page);
              this.$message({
                type: "success",
                message: "操作成功!",
              });
            },
            (error) => {
              window.console.log(error);
            }
          );
        })
        .catch(() => {});
    },
    //多选
    selectionChange(list) {
      this.selectionList = list;
    },
  },
};
</script>

<style></style>
